//
//  PxFindPwdResetViewController.m
//  OilCard
//
//  Created by Xu Yefeng on 14-5-24.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxFindPwdResetViewController.h"
#import "LL360CountDownButton.h"

@interface PxFindPwdResetViewController ()

@end

@implementation PxFindPwdResetViewController

- (void)dealloc{
    LLSmallCountDownButton *countDownBtn = [LLSmallCountDownButton countDownButtonWithTimerKey:@"findpwd"];
    [countDownBtn removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    LLDLog(@"viewDidLayoutSubviews:%f", self.view.height);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    LLDLog(@"viewDidLoad:%f", self.view.height);
//    self.title = @"重置密码";
    [self setLogoTitleViewWithTitle:@"重置密码"];
    
    self.contentView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.contentView];
    self.contentView.top = [PxGlobal getBaseHeight:NO];
    
    UIView *blankview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 15)];
    blankview.backgroundColor = [UIColor colorWithHTMLStr:@"f8f8f8"];
    [self.contentView addSubview:blankview];
    
    UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    [self.contentView addSubview:lineview];
    lineview.top = blankview.bottom-1;
    lineview.backgroundColor = [UIColor colorWithHTMLStr:@"cacaca"];
    
    CGFloat top = blankview.bottom;
    
    LLYTTextField *textfield = [PxWidgetCreater getTextField:@"登录名" andPlacehoder:@"请输入登录名 "];
    self.phone = textfield;
    textfield.top = top;
    [self.contentView addSubview:textfield];
    textfield.delegate = self;
    textfield.width = self.view.width - 100;
    
     lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    [self.contentView addSubview:lineview];
    lineview.top = textfield.bottom-1;
    lineview.backgroundColor = [UIColor colorWithHTMLStr:@"cacaca"];
    
    LLSmallCountDownButton *countdownBtn = [LLSmallCountDownButton countDownButtonWithTimerKey:@"findpwd"];
    [countdownBtn stop];
    [self.contentView addSubview:countdownBtn];
    countdownBtn.frame = CGRectMake(textfield.right, textfield.top, 85, textfield.height);
    [countdownBtn setSelector:@selector(sendVerify) withTarget:self];
    
    top = textfield.bottom;
    
    textfield = [PxWidgetCreater getTextField:@"验证码" andPlacehoder:@"请输入验证码"];
    self.verifyCodeInput = textfield;
    textfield.top = top;
    [self.contentView addSubview:textfield];
    textfield.delegate = self;
    textfield.width = self.view.width - 30;
    
    top = textfield.bottom;
    
    lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    [self.contentView addSubview:lineview];
    lineview.top = textfield.bottom-1;
    lineview.backgroundColor = [UIColor colorWithHTMLStr:@"cacaca"];
    
    textfield = [PxWidgetCreater getTextField:@"密码" andPlacehoder:@"请输入密码"];
    textfield.secureTextEntry = YES;
    self.pwdInput = textfield;
    textfield.top = top;
    [self.contentView addSubview:textfield];
    textfield.delegate = self;
    [self.pwdInput linkPreField:self.verifyCodeInput];
    textfield.width = self.view.width - 30;
    
    lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    [self.contentView addSubview:lineview];
    lineview.top = textfield.bottom-1;
    lineview.backgroundColor = [UIColor colorWithHTMLStr:@"cacaca"];
    
    top = textfield.bottom;
    
    textfield = [PxWidgetCreater getTextField:@"确认密码" andPlacehoder:@"请输入确认密码"];
    textfield.secureTextEntry = YES;
    self.pwdConfirmInput = textfield;
    textfield.top = top;
    [self.contentView addSubview:textfield];
    textfield.delegate = self;
    [self.pwdConfirmInput linkPreField:self.pwdInput];
    textfield.width = self.view.width - 30;
    
    lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    [self.contentView addSubview:lineview];
    lineview.top = textfield.bottom-1;
    lineview.backgroundColor = [UIColor colorWithHTMLStr:@"cacaca"];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.layer.cornerRadius = 4;
    btn2.frame = CGRectMake(35, self.pwdConfirmInput.bottom + 22, CGRectGetWidth(self.contentView.frame) - 70, 38);
    btn2.contentMode = UIViewContentModeLeft;
    [btn2 setSelector:@selector(checkVerify) withTarget:self];
    [btn2 setTitle:@"确认"];
    [btn2 setTitleColor:[UIColor colorWithHTMLStr:@"2f86e9"] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:18];
    btn2.layer.borderColor = [UIColor colorWithHTMLStr:@"2f86e9"].CGColor;
    btn2.layer.borderWidth = 1;
    [self.contentView addSubview:btn2];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    LLDLog(@"viewDidAppear:%f", self.view.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)sendVerify{
    LLSmallCountDownButton *countdownBtn = [LLSmallCountDownButton countDownButtonWithTimerKey:@"findPWD"];
    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
    cmd.functionName = TRANS_USER_SMS_SEND;
    [cmd.paramDic addEntriesFromDictionary:@{@"phone":self.phone.text,
                                            @"type":@"f"}];
    
    cmd.businessSuccessHandler = ^(NSDictionary* dic)
    {
        [LLAlertService alertWithTip:@"验证码已发送，请查收"];
        [countdownBtn start];
    };
    
    cmd.businessErrorHandler = ^(NSDictionary* dic)
    {
        [LLAlertService alertWithTip:dic[RESULT_DESC]];
    };
    
    cmd.errorHandler = ^(NSError* error)
    {
        
    };
    
    [LLRestEngine sendCommandRequest:cmd];
}

- (void)checkVerify{
    if (self.verifyCodeInput.text.length < 1) {
        [LLAlertService alertWithTip:@"验证码不能为空"];
        return;
    }
    if (self.pwdInput.text.length < 1) {
        [LLAlertService alertWithTip:@"密码不能为空"];
        return;
    }
    if (![self.pwdInput.text isEqualToString:self.pwdConfirmInput.text]) {
        [LLAlertService alertWithTip:@"两次密码不一致"];
        return;
    }
    
    [self findBackPwd];
}

- (void)findBackPwd{
    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
    cmd.functionName = TRANS_USER_PWD_FIND;
    [cmd.paramDic addEntriesFromDictionary:@{@"phone":self.phone.text,
                                             @"code":self.verifyCodeInput.text,
                                             @"password":self.pwdConfirmInput.text}];
    
    cmd.businessSuccessHandler = ^(NSDictionary* dic)
    {
        //PxUserInfoBean *bean = weakCmd.userInfoBean;
        
        [LLAlertService alertWithTip:@"修改成功"];
        [self popToViewController:@"PxLoginViewController"];
    };
    
    cmd.businessErrorHandler = ^(NSDictionary* dic)
    {
        [LLAlertService alertWithTip:dic[RESULT_DESC]];
    };
    
    cmd.errorHandler = ^(NSError* error)
    {
        
    };
    
    [LLRestEngine sendCommandRequest:cmd];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

@end
